Load all required libraries.
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'tidyr' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.6, n = 331)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.00438 13.00202 12.99973 12.99750 12.99532 12.99320 12.99112 12.98908
## [9] 12.98707 12.98508 12.98312 12.98118 12.97924 12.97731 12.97537 12.97343
## [17] 12.97148 12.96950 12.96751 12.96548 12.96341 12.96130 12.95915 12.95694
## [25] 12.95467 12.95234 12.94994 12.94745 12.94489 12.94226 12.93957 12.93683
## [33] 12.93405 12.93123 12.92837 12.92548 12.92257 12.91963 12.91669 12.91372
## [41] 12.91076 12.90779 12.90482 12.90187 12.89892 12.89599 12.89309 12.89021
## [49] 12.88736 12.88455 12.88178 12.87906 12.87639 12.87377 12.87121 12.86872
## [57] 12.86630 12.86395 12.86168 12.85949 12.85740 12.85540 12.85349 12.85169
## [65] 12.84987 12.84791 12.84583 12.84363 12.84133 12.83893 12.83646 12.83391
## [73] 12.83130 12.82865 12.82595 12.82324 12.82050 12.81777 12.81503 12.81232
## [81] 12.80964 12.80700 12.80441 12.80188 12.79943 12.79706 12.79478 12.79262
## [89] 12.79057 12.78865 12.78687 12.78525 12.78378 12.78249 12.78139 12.78048
## [97] 12.77978 12.77929 12.77904 12.77862 12.77768 12.77627 12.77442 12.77218
## [105] 12.76961 12.76674 12.76362 12.76031 12.75684 12.75326 12.74961 12.74595
## [113] 12.74232 12.73877 12.73534 12.73207 12.72902 12.72623 12.72375 12.72162
## [121] 12.71988 12.71860 12.71780 12.71754 12.71787 12.71882 12.72045 12.72280
## [129] 12.72646 12.73186 12.73882 12.74715 12.75667 12.76719 12.77854 12.79053
## [137] 12.80297 12.81569 12.82850 12.84121 12.85365 12.86563 12.87696 12.88747
## [145] 12.89696 12.90526 12.91219 12.92026 12.93174 12.94608 12.96272 12.98111
## [153] 13.00069 13.02090 13.04120 13.06102 13.07980 13.09700 13.11206 13.12442
## [161] 13.13352 13.14175 13.15170 13.16318 13.17598 13.18988 13.20468 13.22018
## [169] 13.23615 13.25240 13.26872 13.28490 13.30072 13.31599 13.33050 13.34403
## [177] 13.35638 13.36733 13.37670 13.38425 13.38979 13.39311 13.39550 13.39834
## [185] 13.40152 13.40493 13.40849 13.41209 13.41562 13.41900 13.42211 13.42485
## [193] 13.42714 13.42886 13.42991 13.43020 13.42962 13.42808 13.42547 13.42169
## [201] 13.41664 13.41022 13.40233 13.39287 13.38097 13.36608 13.34858 13.32883
## [209] 13.30723 13.28415 13.25995 13.23503 13.20975 13.18450 13.15964 13.13556
## [217] 13.11264 13.09124 13.07175 13.05113 13.02640 12.99803 12.96647 12.93221
## [225] 12.89569 12.85739 12.81776 12.77728 12.73641 12.69561 12.65534 12.61608
## [233] 12.57828 12.54240 12.50893 12.47831 12.45101 12.42750 12.40531 12.38182
## [241] 12.35727 12.33189 12.30592 12.27959 12.25313 12.22679 12.20080 12.17539
## [249] 12.15080 12.12725 12.10500 12.08427 12.06529 12.04831 12.03389 12.02219
## [257] 12.01280 12.00532 11.99935 11.99448 11.99032 11.98645 11.98247 11.97799
## [265] 11.97260 11.96590 11.95748 11.94694 11.93556 11.92486 11.91478 11.90526
## [273] 11.89627 11.88775 11.87965 11.87190 11.86448 11.85731 11.85036 11.84356
## [281] 11.83688 11.83025 11.82362 11.81695 11.81018 11.80327 11.79615 11.78912
## [289] 11.78249 11.77623 11.77031 11.76469 11.75935 11.75425 11.74935 11.74464
## [297] 11.74008 11.73563 11.73126 11.72695 11.72267 11.71850 11.71457 11.71087
## [305] 11.70739 11.70411 11.70104 11.69817 11.69548 11.69297 11.69063 11.68846
## [313] 11.68643 11.68456 11.68281 11.68120 11.67971 11.67821 11.67660 11.67491
## [321] 11.67317 11.67144 11.66973 11.66809 11.66656 11.66517 11.66395 11.66294
## [329] 11.66218 11.66170 11.66155
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.6, n = 331)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62561 12.62106 12.61662 12.61230 12.60809 12.60398 12.59998 12.59607
## [9] 12.59227 12.58855 12.58492 12.58137 12.57791 12.57452 12.57120 12.56795
## [17] 12.56477 12.56165 12.55858 12.55557 12.55261 12.54969 12.54682 12.54398
## [25] 12.54118 12.53841 12.53566 12.53294 12.53024 12.52755 12.52489 12.52226
## [33] 12.51965 12.51708 12.51455 12.51206 12.50961 12.50721 12.50487 12.50258
## [41] 12.50035 12.49819 12.49610 12.49408 12.49213 12.49026 12.48848 12.48678
## [49] 12.48518 12.48367 12.48225 12.48094 12.47974 12.47865 12.47767 12.47680
## [57] 12.47606 12.47544 12.47496 12.47460 12.47438 12.47430 12.47437 12.47458
## [65] 12.47493 12.47539 12.47597 12.47667 12.47748 12.47839 12.47943 12.48057
## [73] 12.48182 12.48318 12.48464 12.48621 12.48789 12.48967 12.49156 12.49354
## [81] 12.49563 12.49781 12.50010 12.50248 12.50495 12.50753 12.51019 12.51296
## [89] 12.51581 12.51875 12.52179 12.52491 12.52812 12.53142 12.53480 12.53827
## [97] 12.54182 12.54545 12.54917 12.55296 12.55684 12.56079 12.56482 12.56893
## [105] 12.57311 12.57737 12.58170 12.58610 12.59057 12.59511 12.59972 12.60440
## [113] 12.60914 12.61395 12.61882 12.62376 12.62876 12.63382 12.63894 12.64412
## [121] 12.64936 12.65465 12.66001 12.66541 12.67087 12.67638 12.68225 12.68873
## [129] 12.69575 12.70322 12.71106 12.71920 12.72755 12.73604 12.74458 12.75310
## [137] 12.76152 12.76976 12.77774 12.78538 12.79260 12.79932 12.80732 12.81821
## [145] 12.83167 12.84736 12.86496 12.88414 12.90457 12.92592 12.94787 12.97009
## [153] 12.99225 13.01402 13.03507 13.05508 13.07372 13.09065 13.10556 13.11811
## [161] 13.12798 13.13655 13.14543 13.15456 13.16392 13.17345 13.18313 13.19290
## [169] 13.20274 13.21261 13.22246 13.23226 13.24196 13.25153 13.26092 13.27011
## [177] 13.27905 13.28769 13.29601 13.30396 13.31151 13.31861 13.32522 13.33131
## [185] 13.33684 13.34176 13.34604 13.34964 13.35252 13.35464 13.35597 13.35645
## [193] 13.35606 13.35476 13.35250 13.34924 13.34495 13.33959 13.33312 13.32549
## [201] 13.31668 13.30664 13.29533 13.28271 13.26683 13.24623 13.22158 13.19356
## [209] 13.16283 13.13007 13.09596 13.06117 13.02638 12.99226 12.95949 12.92874
## [217] 12.90069 12.87601 12.85537 12.83514 12.81149 12.78483 12.75557 12.72412
## [225] 12.69089 12.65630 12.62074 12.58464 12.54839 12.51242 12.47712 12.44291
## [233] 12.41020 12.37940 12.35092 12.32516 12.30255 12.28348 12.26674 12.25084
## [241] 12.23573 12.22138 12.20775 12.19482 12.18254 12.17088 12.15981 12.14929
## [249] 12.13928 12.12976 12.12068 12.11201 12.10373 12.09578 12.08814 12.08078
## [257] 12.07365 12.06673 12.05997 12.05335 12.04683 12.04038 12.03395 12.02752
## [265] 12.02104 12.01450 12.00784 12.00104 11.99439 11.98819 11.98244 11.97712
## [273] 11.97220 11.96769 11.96357 11.95982 11.95642 11.95337 11.95064 11.94824
## [281] 11.94613 11.94431 11.94276 11.94147 11.94043 11.93961 11.93901 11.93862
## [289] 11.93841 11.93837 11.93849 11.93876 11.93916 11.93968 11.94030 11.94101
## [297] 11.94180 11.94264 11.94353 11.94445 11.94539 11.94640 11.94753 11.94879
## [305] 11.95017 11.95168 11.95333 11.95511 11.95702 11.95907 11.96126 11.96358
## [313] 11.96605 11.96867 11.97142 11.97433 11.97739 11.98059 11.98395 11.98747
## [321] 11.99114 11.99497 11.99896 12.00311 12.00742 12.01190 12.01655 12.02137
## [329] 12.02635 12.03151 12.03685
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.6, n = 331)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.99783 11.99409 11.99047 11.98694 11.98352 11.98019 11.97695 11.97378
## [9] 11.97069 11.96766 11.96469 11.96177 11.95890 11.95606 11.95326 11.95049
## [17] 11.94773 11.94498 11.94224 11.93950 11.93675 11.93399 11.93120 11.92839
## [25] 11.92554 11.92266 11.91972 11.91673 11.91368 11.91056 11.90736 11.90409
## [33] 11.90072 11.89726 11.89370 11.89004 11.88628 11.88244 11.87855 11.87460
## [41] 11.87060 11.86657 11.86250 11.85842 11.85432 11.85021 11.84611 11.84202
## [49] 11.83795 11.83391 11.82991 11.82595 11.82204 11.81819 11.81441 11.81071
## [57] 11.80710 11.80357 11.80016 11.79685 11.79366 11.79059 11.78766 11.78488
## [65] 11.78225 11.77977 11.77747 11.77534 11.77340 11.77164 11.77009 11.76840
## [73] 11.76624 11.76365 11.76066 11.75731 11.75363 11.74967 11.74544 11.74100
## [81] 11.73637 11.73159 11.72670 11.72173 11.71671 11.71168 11.70669 11.70175
## [89] 11.69691 11.69220 11.68766 11.68332 11.67922 11.67540 11.67188 11.66871
## [97] 11.66591 11.66353 11.66160 11.66016 11.65923 11.65886 11.65908 11.65993
## [105] 11.66144 11.66365 11.66629 11.66908 11.67205 11.67519 11.67853 11.68208
## [113] 11.68585 11.68985 11.69410 11.69860 11.70337 11.70843 11.71378 11.71945
## [121] 11.72543 11.73175 11.73841 11.74544 11.75283 11.76062 11.76880 11.77739
## [129] 11.78768 11.80078 11.81639 11.83421 11.85397 11.87536 11.89809 11.92188
## [137] 11.94644 11.97147 11.99667 12.02177 12.04647 12.07047 12.09350 12.11524
## [145] 12.13543 12.15376 12.16994 12.18749 12.20962 12.23557 12.26459 12.29594
## [153] 12.32885 12.36257 12.39636 12.42946 12.46111 12.49056 12.51706 12.53986
## [161] 12.55821 12.57525 12.59449 12.61562 12.63838 12.66249 12.68766 12.71363
## [169] 12.74011 12.76682 12.79350 12.81985 12.84560 12.87047 12.89419 12.91648
## [177] 12.93706 12.95564 12.97196 12.98573 12.99668 13.00453 13.01061 13.01640
## [185] 13.02185 13.02691 13.03153 13.03565 13.03922 13.04218 13.04449 13.04610
## [193] 13.04694 13.04696 13.04612 13.04436 13.04163 13.03787 13.03303 13.02707
## [201] 13.01992 13.01153 13.00185 12.99084 12.97624 12.95634 12.93188 12.90360
## [209] 12.87222 12.83849 12.80314 12.76691 12.73053 12.69473 12.66025 12.62783
## [217] 12.59821 12.57211 12.55027 12.52855 12.50263 12.47301 12.44018 12.40464
## [225] 12.36687 12.32737 12.28663 12.24515 12.20341 12.16192 12.12116 12.08163
## [233] 12.04382 12.00823 11.97534 11.94564 11.91965 11.89784 11.87846 11.85944
## [241] 11.84078 11.82246 11.80450 11.78687 11.76959 11.75264 11.73603 11.71975
## [249] 11.70379 11.68816 11.67285 11.65785 11.64316 11.62879 11.61595 11.60562
## [257] 11.59742 11.59097 11.58587 11.58176 11.57824 11.57493 11.57144 11.56741
## [265] 11.56244 11.55614 11.54814 11.53806 11.52709 11.51667 11.50676 11.49731
## [273] 11.48828 11.47963 11.47131 11.46329 11.45551 11.44793 11.44052 11.43322
## [281] 11.42599 11.41880 11.41159 11.40433 11.39697 11.38947 11.38178 11.37422
## [289] 11.36710 11.36036 11.35397 11.34786 11.34200 11.33633 11.33081 11.32539
## [297] 11.32001 11.31463 11.30921 11.30369 11.29803 11.29236 11.28688 11.28156
## [305] 11.27640 11.27138 11.26648 11.26171 11.25705 11.25248 11.24799 11.24358
## [313] 11.23923 11.23492 11.23066 11.22642 11.22219 11.21780 11.21311 11.20818
## [321] 11.20304 11.19775 11.19237 11.18693 11.18149 11.17610 11.17080 11.16565
## [329] 11.16069 11.15597 11.15155
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")